﻿/****************************** Module Header ******************************\
 * Module Name:  IconHelper.cs
 * Module Version:  1.0.0 (2013-6-6)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to operate the file or the directory. It supplies some
 * public method to move a folder,copy a folder,replace a file and so on.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace UCL
{
    /// <summary>
    /// 图标助手
    /// 2013-6-6 by sutroon
    /// <para>注意：</para>
    /// <para>1、如果抽取的图标显示失真，把imageList1的属性ColorDepth值改为Depth32Bit即可解决该问题。</para>
    /// <para>2、我们保存为ico格式的图标将会严重失真，系统提供的save其实只能保存为png格式，如果确实需要保存为ico格式的话，需要自己去找个开源的代码写入。</para>
    /// </summary>
    public class IconHelper
    {

        [System.Runtime.InteropServices.DllImport("shell32.dll")]
        private static extern int ExtractIconEx(string lpszFile, int niconIndex, IntPtr[] phiconLarge, IntPtr[] phiconSmall, int nIcons);

        /// <summary>
        /// 从exe文件抽取所有图标
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static IList<Bitmap> ExtractIcon(string filePath)
        {
            IntPtr[] largeIcons, smallIcons;  //存放大/小图标的指针数组 

            //第一步：获取程序中的图标数  
            int IconCount = ExtractIconEx(filePath, -1, null, null, 0);

            //第二步：创建存放大/小图标的空间  
            largeIcons = new IntPtr[IconCount];
            smallIcons = new IntPtr[IconCount];

            //第三步：抽取所有的大小图标保存到largeIcons和smallIcons中
            ExtractIconEx(filePath, 0, largeIcons, smallIcons, IconCount);

            //第四步：显示抽取的图标(推荐使用imageList和listview搭配显示)
            List<Bitmap> imgList = new List<Bitmap>();
            for (int i = 0; i < IconCount; i++)
            {
                imgList.Add(Icon.FromHandle(largeIcons[i]).ToBitmap()); //图标添加进imageList中  

            }
            return imgList;
        }

        /// <summary>
        /// 从exe文件抽取第一个图标
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Bitmap ExtractFirstIcon(string filePath)
        {
            IntPtr[] largeIcons, smallIcons;  //存放大/小图标的指针数组 

            //第一步：获取程序中的图标数  
            int IconCount = ExtractIconEx(filePath, -1, null, null, 0);

            //第二步：创建存放大/小图标的空间  
            largeIcons = new IntPtr[IconCount];
            smallIcons = new IntPtr[IconCount];

            //第三步：抽取所有的大小图标保存到largeIcons和smallIcons中
            ExtractIconEx(filePath, 0, largeIcons, smallIcons, IconCount);

            //第四步：显示抽取的图标(推荐使用imageList和listview搭配显示)
            for (int i = 0; i < IconCount; i++)
            {
                return Icon.FromHandle(largeIcons[i]).ToBitmap();

            }
            return null;
        }

        /// <summary>
        /// 获取文件类型的默认图标
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static Bitmap ExtractAssociatedIcon(string filePath)
        {
            return Icon.ExtractAssociatedIcon(filePath).ToBitmap();
        }
    }
}
